package com.wolf.util;

/**
 * @Author huangzy
 * @Date 2019/12/02 16:22
 */
import org.springframework.util.SocketUtils;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;


public class WolfSocketUtils {

    /**
     * 判断某服务能否连通
     *
     * @param host host
     * @param port port
     * @return boolean
     */
    public static boolean isRunning(String host, int port) {
        Socket sClient = null;
        try {
            SocketAddress saAdd = new InetSocketAddress(host.trim(), port);
            sClient = new Socket();
            //设置超时 3s
            sClient.connect(saAdd, 3000);
        } catch (IOException e) {
            return false;
        } finally {
            try {
                if (sClient != null) {
                    sClient.close();
                }
            } catch (Exception e) {
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isRunning("172.17.28.138", 8090));
        SocketUtils.findAvailableTcpPort(80,8090);
    }
}